package com.jysz.certificate.action;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.google.gson.Gson;
import com.google.gson.internal.StringMap;
import com.google.gson.reflect.TypeToken;
import com.jysz.certificate.Constants;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.service.system.OrganizationService;
import com.jysz.certificate.service.system.RoleService;
import com.jysz.certificate.service.system.ShopService;
import com.jysz.webcore.ssh.web.struts2.PageAction;

/**
 * @author kacarich email:javaengine@outlook.com
 *
 */
@ParentPackage("base-package")
public abstract class AbstractBaseAction extends PageAction {

	private static final long serialVersionUID = 1L;
	public static final String DELETE="delete";
	private String jumpMehtod = "jumpPage";

	@Resource
	private ShopService shopService;
	@Resource
	private RoleService roleService;
	@Resource
	private OrganizationService orgService;
	
	public User getUser() {
		return super.getCurrentSession() == null ? null : (User) super.getCurrentSession();
	}

	public String getRealPath(String webPath) {
		return getServletContext().getRealPath(webPath);
	}

	public String getRealWebRoot() {
		return getServletContext().getRealPath(String.valueOf(Constants.FILE_SPT));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Enum getEnumVal(String enumType, String name) {
		Class enumClass = null;
		try {
			enumClass = Class.forName(enumType);
			return Enum.valueOf(enumClass, name);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new IllegalArgumentException(e);
		}
	}

	public boolean hasOper(String operCode) {
		User user = getUser();
		if (user == null) {
			return false;
		}
		return roleService.checkPermission(user, operCode);
	}
	
	public String getJumpMehtod() {
		return jumpMehtod;
	}

	public void setJumpMehtod(String jumpMehtod) {
		this.jumpMehtod = jumpMehtod;
	}
	
	
	@SuppressWarnings({ "unchecked", "unused" })
	private  String buildParamStr(String searchObjName, Object o){
		
		if(searchObjName == null)
			searchObjName = "search";
		StringBuilder sb = new StringBuilder();
		Gson gson = new Gson();
		
		Map<String, Object> map = gson.fromJson(gson.toJson(o),
                new TypeToken<Map<String, Object>>() {}.getType());
		
		for(String s:map.keySet()){
			if(map.get(s).getClass().equals(StringMap.class)){
				build((StringMap<Object>) map.get(s), sb, searchObjName+"."+s);
			}else{
				sb.append("&").append(searchObjName).append(".").append(s).append("=").append(map.get(s));
			}
		}
		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	private  void build(StringMap<Object> sm, StringBuilder sb, String preStr){
		for(String s:sm.keySet()){
			if(sm.get(s).getClass().equals(StringMap.class)){
				preStr=preStr+"."+s;
				build((StringMap<Object>)sm.get(s), sb,preStr);
			}else{
				sb.append("&").append(preStr).append(".").append(s).append("=").append(sm.get(s));
			}
		}
	}
	
	/**
	 * 当前用户是否是驻店员
	 * @return
	 */
	public boolean isShoper(){
		
		return PostType.Shop.equals(this.getUser().getPost());
	}
	
	/**
	 * 当前用户是否是区域经理
	 * @return
	 */
	public boolean isAreaManager(){
		
		return PostType.Manager.equals(this.getUser().getPost());
	}
	
	public List<Shop> getCurrentUserShops(){
		return shopService.findShopsByCurrentUser();
	}
	
	/*
	 * 获得区域经理名字
	 */
	public String getShopUserName(Long shopId){
		StringBuilder names = new StringBuilder();
		List<Organization> orgs = orgService.findUserDomainByShopId(shopId);
		if(orgs != null && orgs.size() > 0){
			for (Iterator<Organization> iterator = orgs.iterator(); iterator.hasNext();) {
				Organization org = iterator.next();
				names.append(org.getName());
				if(iterator.hasNext()){
					names.append(",");
				}
			}
		}
		return names.toString();
	}
	
	/*
	 * 获得驻店员名字
	 */
	public String findUserShopByShopId(Long shopId){
		StringBuilder names = new StringBuilder();
		List<Organization> orgs = orgService.findUserShopByShopId(shopId);
		if(orgs != null && orgs.size() > 0){
			for (Iterator<Organization> iterator = orgs.iterator(); iterator.hasNext();) {
				Organization org = iterator.next();
				names.append(org.getName());
				if(iterator.hasNext()){
					names.append(",");
				}
			}
		}
		return names.toString();
	}
	
	private final static String formatAmountStr = "#,##0.00";
	public Format formatAmount = new DecimalFormat(formatAmountStr);
	
	public String formatAmount(Object number){
		if(number == null){
			return "0.00";
		}
		return formatAmount.format(number);
	}
}
